let myHook = require('./myHook.js');

class HookCodeFactory {
  args() {
    return this.options.args.join(',')
  }

  head() {
    return `var _x=this._x;`
  }

  content() {
    let code = ''
    for (var i = 0; i < this.options.taps.length; i++) {
      code += `var _fn${i}=_x[${i}];_fn${i}(${this.args()});`
    }
    return code
  }

  setup(instance, options) {
    this.options = options
    instance._x = options.taps.map(o => o.fn);
  }

  create() {// 创建可执行代码
    let fn
    fn = new Function(
      this.args(),
      this.head() + this.content()
    )
    return fn;
  }
}

let factory = new HookCodeFactory();
class mySyncHook extends myHook {
  constructor(args) {
    super(args)
  }

  compile(options) {// {taps:[{},{}],args:[name,age]}
    factory.setup(this, options);
    return factory.create(options);
  }
}

module.exports = mySyncHook